<?php
/*
 * CakePHP Ajax Chat Plugin (using jQuery);
 * Copyright (c) 2008 Matt Curry
 * www.PseudoCoder.com
 * http://github.com/mcurry/cakephp/tree/master/plugins/chat
 * http://sandbox2.pseudocoder.com/demo/chat
 *
 * @author      Matt Curry <matt@pseudocoder.com>
 * @license     MIT
 *
 */

if (!defined('CHAT_FREQUENCY')) {
	define('CHAT_FREQUENCY', 5);
}

class AjaxChatHelper extends Helper {
	var $helpers = array('Html', 'Javascript', 'Form','Ajax');

	/*
	key is an identifier for the chat.
	You can have multiple chats by using different keys.
	*/
     function generate($key, $user = null, $place =null) {
	$id = sprintf('chat_%s', $key);
		
	$js1 = '';
	$js1 .= 'function add_msg() {';
	$js1 .= 'if (document.getElementById("ChatMessage").value != "") {';
	$js1 .= 'var msgContainer = document.createElement("p");';
	$js1 .= 'msgContainer.setAttribute("class", "new");';
	$js1 .= 'var nameContainer = document.createElement("strong");';


	$js1 .= 'var nameNode = document.createTextNode(document.getElementById("ChatName").value + " (now): ");';
	$js1 .= 'var msgNode = document.createTextNode(document.getElementById("ChatMessage").value);';

	$js1 .= 'nameContainer.appendChild(nameNode);';
	$js1 .= 'msgContainer.appendChild(nameContainer);';
	$js1 .= 'msgContainer.appendChild(msgNode);';
	$js1 .= 'document.getElementById("updateChatWindow").appendChild(msgContainer);';
	$js1 .= 'var objDiv = document.getElementById("chat_window");';
	$js1 .= 'objDiv.scrollTop = objDiv.scrollHeight;';
	$js1 .= '}';
	$js1 .= '}';

	$js1 .="function updateChatList() {
			new Ajax.Updater('chat_".$key."','/mastergoal-uca/Mastergoal/chat/update/".$key."/".$user['user']."/".$place."', {asynchronous:true, evalScripts:true, requestHeaders:['X-Update', 'chat_".$key."']});
		}";
	
	$output ='';
	$output .= $this->Javascript->codeBlock($js1, false);	
	$html = array();
	$html[] = '<div class="chat">';
	$html[] = '<table><tr><th scope="col"><strong>CHAT</strong></th></tr> ';
	//$html[] = '<tr class="listaChat"><td><div id="chat_window"><div id="updateChatWindow"><p>Loading...</p></div></div></td></tr>';
	$html[] = '<tr class="listaChat"><td><div id="chat_window"><div id="updateChatWindow"><p>';
	$html[] = $this->Html->image("loading/ajax-loader.gif", array('alt' => 'Loading'));
	$html[] = '</p></div></div></td></tr>';
	if ($user['user'] != null){
		$html[]= '<tr class="mensajeChat"><td>';
		$html[] = $this->Ajax->form('chat/post','post',array('model'=>'Chat','url' => array(
				'controller' => 'chat',
				'action' => 'post'),
				'before' => 'add_msg()',
				'after' => 'document.getElementById("ChatMessage").value=""'));
		$html[] = $this->Form->input('key', array('type' => 'hidden', 'value' => $key));
		$html[] = $this->Form->input('name', array('id' => 'ChatName','type' => 'hidden', 'value' => $user['username']));
		$html[] = $this->Form->input('message', array('label'=>'','id' => 'ChatMessage','type' => 'textarea'));
		$html[] = $this->Form->end('Aceptar');
		$html[] = '</div></td></tr>';
	}
	$html[]='</table>';
	
	//implode: Une elementos de una matriz mediante una cadena
	$htmlFinal = '';
	$htmlFinal= "\n" . implode("\n", $html);
	$output.= $htmlFinal;
	$js = '';
	//setup the timer that will update the chat window

	//$js.="new Ajax.PeriodicalUpdater( 'updateChatWindow', '/mastergoal-uca/Mastergoal/chat/update/".$key."/".$user['user']."/".$place."', {method: 'http post', frequency: 7, decay: 2});";
	
	
	$js.= "new PeriodicalExecuter(function(pechat) {
		new Ajax.Updater('updateChatWindow', '/mastergoal-uca/Mastergoal/chat/update/".$key."/".$user['user']."/".$place."', {
			method: 'http post',
			onComplete: function() {
				var objDiv = document.getElementById('chat_window');
				objDiv.scrollTop = objDiv.scrollHeight;
			      }
		});
	      }, 7);";

	/*$js.="new Ajax.PeriodicalUpdater( 'updateChatWindow', '/mastergoal-uca/Mastergoal/chat/update/".$key."/".$user['user']."/".$place."', {method: 'http post', frequency: 7, decay: 2,  onComplete: function() {
	var objDiv = document.getElementById('chat_window');
	objDiv.scrollTop = objDiv.scrollHeight;
	alert('actualizado');
	 }});";*/
	
	$output .= $this->Javascript->codeBlock($js,false);
	
	return $output;
	}

}
?>